package com.ruoyi.car.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.car.domain.CarConfig;
import com.ruoyi.car.service.ICarConfigService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 车辆-配置信息Controller
 *
 * @author liujinlei
 * @date 2022-03-06
 */
@RestController
@RequestMapping("/config")
public class CarConfigController extends BaseController {
    @Autowired
    private ICarConfigService carConfigService;

    /**
     * 查询车辆-配置信息列表
     */
    @RequiresPermissions("car:config:list")
    @GetMapping("/list")
    public TableDataInfo list(CarConfig carConfig) {
        startPage();
        List<CarConfig> list = carConfigService.selectCarConfigList(carConfig);
        for (CarConfig config : list) {
            config.setInfoJson(JSON.parseObject(config.getInfo()));
        }
        return getDataTable(list);
    }

    /**
     * 导出车辆-配置信息列表
     */
    @RequiresPermissions("car:config:export")
    @Log(title = "车辆-配置信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarConfig carConfig) {
        List<CarConfig> list = carConfigService.selectCarConfigList(carConfig);
        ExcelUtil<CarConfig> util = new ExcelUtil<CarConfig>(CarConfig.class);
        util.exportExcel(response, list, "车辆-配置信息数据");
    }

    /**
     * 获取车辆-配置信息详细信息
     */
    @RequiresPermissions("car:config:query")
    @GetMapping(value = "/{carConfigId}")
    public AjaxResult getInfo(@PathVariable("carConfigId") Long carConfigId) {
        return AjaxResult.success(carConfigService.selectCarConfigByCarConfigId(carConfigId));
    }

    /**
     * 新增车辆-配置信息
     */
    @RequiresPermissions("car:config:add")
    @Log(title = "车辆-配置信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarConfig carConfig) {
        carConfig.setInfo(JSON.toJSONString(carConfig.getInfoJson()));
        return toAjax(carConfigService.insertCarConfig(carConfig));
    }

    /**
     * 修改车辆-配置信息
     */
    @RequiresPermissions("car:config:edit")
    @Log(title = "车辆-配置信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarConfig carConfig) {
        carConfig.setInfo(JSON.toJSONString(carConfig.getInfoJson()));
        return toAjax(carConfigService.updateCarConfig(carConfig));
    }

    /**
     * 删除车辆-配置信息
     */
    @RequiresPermissions("car:config:remove")
    @Log(title = "车辆-配置信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{carConfigIds}")
    public AjaxResult remove(@PathVariable Long[] carConfigIds) {
        return toAjax(carConfigService.deleteCarConfigByCarConfigIds(carConfigIds));
    }
}
